package cn.icanci.order.application;

import cn.icanci.basic.commonutils.R;
import cn.icanci.order.form.PreOrderForm;
import cn.icanci.order.service.PreOrderService;
import cn.icanci.order.spi.TradeLogService;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.*;

/**
 * @author icanci
 * @date Created in 2021/05/15 14:43
 * @version 1.0
 */
@RestController
@Slf4j
@RequestMapping("/order")
public class OrderApplication {
    @Resource
    private TradeLogService tradeLogService;
    @Resource
    private PreOrderService preOrderService;

    @PostMapping("/preOrder")
    public R preOrder(@RequestBody PreOrderForm preOrderForm) {
        try {
            tradeLogService.setLogInfo("ORDER_BOOK", "PRE_ORDER", "");
            tradeLogService.info(log, "用户下单，下单信息为:{}", preOrderForm);
            return preOrderService.preBook(preOrderForm);
        } finally {
            tradeLogService.removeAll();
        }
    }

    @GetMapping("/bookPay/{orderId}")
    public R bookPay(@PathVariable("orderId") Long orderId) {
        try {
            tradeLogService.setLogInfo("ORDER_BOOK", "BOOK_PAY", "");
            tradeLogService.info(log, "用户付款，下单信息为:{}", orderId);
            return preOrderService.book(orderId);
        } finally {
            tradeLogService.removeAll();
        }
    }

    @GetMapping("/cancelOrder/{orderId}")
    public R cancelOrder(@PathVariable("orderId") Long orderId) {
        try {
            tradeLogService.setLogInfo("ORDER_BOOK", "CANCEL_ORDER", "");
            tradeLogService.info(log, "取消订单:{}", orderId);
            return preOrderService.cancelOrder(orderId);
        } finally {
            tradeLogService.removeAll();
        }
    }

    @GetMapping("/queryManyRowByUserPhone/{phoneNumber}")
    public R queryManyRowByUserPhone(@PathVariable("phoneNumber") String phoneNumber) {
        try {
            tradeLogService.setLogInfo("ORDER_BOOK", "BOOK_PAY", "");
            tradeLogService.info(log, "用户查询订单:{}", phoneNumber);
            return preOrderService.queryManyRowByUserPhone(phoneNumber);
        } finally {
            tradeLogService.removeAll();
        }
    }

    @GetMapping("/queryOneRowByUserPhone/{orderId}")
    public R queryOneRowByUserPhone(@PathVariable("orderId") Long orderId) {
        try {
            tradeLogService.setLogInfo("ORDER_BOOK", "BOOK_PAY", "");
            tradeLogService.info(log, "用户查询订单:{}", orderId);
            return preOrderService.queryOneRowByUserPhone(orderId);
        } finally {
            tradeLogService.removeAll();
        }
    }

    @GetMapping("/queryManyRowByAdmin")
    public R queryManyRowByAdmin() {
        try {
            tradeLogService.setLogInfo("ORDER_BOOK", "BOOK_PAY", "");
            tradeLogService.info(log, "管理员查询订单");
            return preOrderService.queryManyRowByAdmin();
        } finally {
            tradeLogService.removeAll();
        }
    }

    @GetMapping("/queryManyToAnalysisByAdmin")
    public R queryManyToAnalysisByAdmin() {
        try {
            tradeLogService.setLogInfo("ORDER_BOOK", "ORDER_ANALYSIS", "");
            tradeLogService.info(log, "管理员分析订单");
            return preOrderService.queryManyToAnalysisByAdmin();
        } finally {
            tradeLogService.removeAll();
        }
    }
}
